#include "config.h"

#include "ir_handle.h"

IR_CODE xdata ir_code; //红外遥控信息

#define TIM4CNT 0 //(65536UL - (MAIN_Fosc / 1000))
// //红外解码初始化函数
// void IrTask_Init(void)
// {
// 	GPIO_Init(GPIOD, GPIO_PIN_7, GPIO_MODE_IN_FL_IT);
// 	//GPIO_Init(GPIOB, GPIO_PIN_7, GPIO_MODE_IN_PU_IT);
// 	EXTI_DeInit();
// 	EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOD, EXTI_SENSITIVITY_FALL_ONLY);
// 	//EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOB,EXTI_SENSITIVITY_FALL_ONLY);
// 	//EXTI_SetTLISensitivity(EXTI_TLISENSITIVITY_FALL_ONLY);
// 	enableInterrupts();
// }
// //定时器初始化函数
// void Init_TIM2(void)
// {
// 	TIM2_DeInit();
// 	TIM2_TimeBaseInit(TIM2_PRESCALER_16, 60000);
// 	TIM2_ClearFlag(TIM2_FLAG_UPDATE);

// 	TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);
// 	Timer4_Run();//使能定时器（TIM2整个模块）
// }

static int TimeCnt;
void SetCounter(unsigned int value)
{
	TimeCnt = 0;
	T4_Load(value);
}

unsigned int GetCounter()
{
	//return TimeCnt * 1000 + ((TH4 * 256 + TL4) - (65536UL - (MAIN_Fosc / 1000))) / 22.1184;
	return TimeCnt * 2962 + ((TH4 * 256 + TL4)) / 22.1184;

	// return   ((TH4 * 256 + TL4) - TIM4CNT) / 22.1184;
	//return (TimeCnt * 1000l + ((TH4 * 256 + TL4) - 43418l)) * 10000l / 221184l;
}

/*定时器溢出中断内处理*/
void TIM_IT_Updata_Handle(void) //2.962ms
{
	if (TimeCnt++ > 20) //120ms 大于20ms即可
	{
		TimeCnt = 0;
		ir_code.Ir_Status = 0;
		SetCounter(TIM4CNT);
		Timer4_Stop();
		//  printf("TIM4\r\n");
	}
}
//红外接收处理函数
u8 Ir_Process(void)
{
	u8 Ir_num = 0;			 //最终处理后键值返回值
	u8 Address_H, Address_L; //地址码，地址反码
	u8 Data_H, Data_L;		 // 数据码，数据反码

	if (ir_code.Ir_receive_ok == 1) //接收完成
	{

		Address_H = ir_code.Ir_Receive_Data >> 24;			  //得到地址码
		Address_L = ((ir_code.Ir_Receive_Data) >> 16) & 0xff; //得到地址反码

		if (Address_H == (u8)~Address_L)
		{
			Data_H = ir_code.Ir_Receive_Data >> 8; //得到数据码
			Data_L = ir_code.Ir_Receive_Data;	   //得到数据反码

			if (Data_H == (u8)~Data_L)
			{
				Ir_num = Data_H; // 正确键值
				ir_code.Ir_receive_ok = 0;
				ir_code.Ir_Receive_Data = 0;
			}
		}
	}

	return Ir_num;
}

//红外接收中断处理函数
void Ir_Receive_Handle(void)
{
	u16 Intervak_tim = 0; //两个下降沿间隔时间

	switch (ir_code.Ir_Status)
	{
	case 0: // 第一个下降沿，定时器开始计数
		// printf("111\r\n");
		ir_code.Ir_Status = 1;
		Timer4_Run();
		SetCounter(TIM4CNT);

		break;

	case 1: // 第二个下降沿，定时器关闭，读取定时器计数值
		// printf("222");
		Timer4_Stop();
		Intervak_tim = 0;
		Intervak_tim = GetCounter(); //读取定时器计数值
		SetCounter(TIM4CNT);
		Timer4_Run();
		// printf("	%u\r\n", Intervak_tim);

		//if(Intervak_tim>=12500 && Intervak_tim<=14500)//判断引导吗是否正确9+4.5ms
		if (Intervak_tim >= 12500 && Intervak_tim <= 16000) //判断引导吗是否正确9+4.5ms
		{
			ir_code.Ir_Status = 2; //进入下一个状态
		}
		else
		{
			ir_code.Ir_Status = 0; //引导码错误，从新接收
			ir_code.Ir_receive_ok = 0;
			ir_code.Ir_Receive_Count = 0;
		}
		break;

	case 2: // 进入32位数据接收
		// printf("333");
		Timer4_Stop();
		Intervak_tim = 0;
		Intervak_tim = GetCounter(); //读取定时器计数值
		SetCounter(TIM4CNT);
		Timer4_Run();
		// printf("	%u", Intervak_tim);

		if (ir_code.Ir_Receive_Count < 32)
		{
			if (Intervak_tim >= 1000 && Intervak_tim <= 1300) //间隔1.12--0
			{
				ir_code.Ir_Receive_Data = ir_code.Ir_Receive_Data << 1;
				ir_code.Ir_Receive_Count++;
				// printf("	0\r\n");
			}
			else if (Intervak_tim >= 2000 && Intervak_tim <= 2600)
			{
				ir_code.Ir_Receive_Data = ir_code.Ir_Receive_Data << 1;
				ir_code.Ir_Receive_Data = ir_code.Ir_Receive_Data | 0x0001;
				ir_code.Ir_Receive_Count++;
				// printf("	1\r\n");
			}
			else
			{
				ir_code.Ir_Status = 0;
				ir_code.Ir_receive_ok = 0;
				ir_code.Ir_Receive_Data = 0;
				ir_code.Ir_Receive_Count = 0;
			}

			if (ir_code.Ir_Receive_Count == 32)
			{
				ir_code.Ir_Status = 0;
				ir_code.Ir_receive_ok = 1;
				ir_code.Ir_Receive_Count = 0;
			}
		}
		else
		{
			ir_code.Ir_Status = 0;
			ir_code.Ir_receive_ok = 1;
			ir_code.Ir_Receive_Count = 0;
		}

		break;

	default:
		break;
	}
}
